<?php
namespace app\system\controller;

use app\common\model\ShopGoodsModel;
use app\common\model\ShopPromotionLimitModel;
use think\Controller;
use think\Request;

class ShopPromotionLimitController extends BaseController
{

    protected $goodsId;
    private $goodsModel;
    private $promotionLimitModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->goodsId             = $this->request->param('goods_id', 0);
        $this->goodsModel          = new ShopGoodsModel();
        $this->promotionLimitModel = new ShopPromotionLimitModel();
    }

    /**
     * 预售商品
     */
    public function index()
    {
        $goodsName = $this->request->param('goods_name');
        $map       = [];
        $query     = [];
        if (!empty($goodsName)) {
            $map['goods_name']   = ['like', '%' . $goodsName . '%'];
            $query['goods_name'] = $goodsName;
        }
        $map['site_id'] = $this->getSiteId();
        $list = $this->promotionLimitModel->alias('t1')
            ->join("shop_goods t2", "t1.goods_id=t2.goods_id")
            ->where($map)->paginate(12, false, ['query' => $query]);
        cookie('__GOODSURL__',get_current_url());
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    /**
     * 删除预售商品
     * @return Controller
     */
    public function del()
    {
        $goodsId = $this->request->param('goods_id/d');
        $this->goodsModel->where('goods_id', $goodsId)->setField('promotion_type', 0);
        $this->promotionLimitModel->delData($goodsId);
        $this->success('删除成功！', cookie('__GOODSURL__'));
    }
}
